/*****************************************************************
 MYD Engine v0.1

 File: myd_timer.cpp
 Created: 09/07/09
*****************************************************************/

#include "myd_timer.h"

using namespace myd;

Timer::Timer ()
:
TimeBetweenFrames(0),
MeasureFpsSample(0),
m_uiFPS(0),
m_uiFrameCounter(0)
{
}

Timer::~Timer ()
{
}

void Timer::firstMeasure()
{
	QueryPerformanceFrequency(&Frequency);
	QueryPerformanceCounter(&PerfCount1);

	TimeBetweenFrames = 0;
	MeasureFpsSample = 0;
	m_uiFPS = 0;
	m_uiFrameCounter = 0;
}

void Timer::measure()
{
	QueryPerformanceCounter(&PerfCount2);
	
	TimeBetweenFrames = static_cast<double>
		((PerfCount2.QuadPart - PerfCount1.QuadPart) *1000.0f / Frequency.QuadPart);
	
	MeasureFpsSample += TimeBetweenFrames;

	m_uiFrameCounter++;
	PerfCount1 = PerfCount2;
	
	if(MeasureFpsSample >= 1000)
	{
		MeasureFpsSample -= 1000;
		m_uiFPS = m_uiFrameCounter;
		m_uiFrameCounter = 0;
	}
}
